{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from numpy import nan as NA\n",
    "\n",
    "np.random.seed(12345)\n",
    "np.set_printoptions(precision=4, suppress=True)\n",
    "pd.options.display.max_rows = 25\n",
    "pd.options.display.max_columns = 20\n",
    "pd.options.display.max_colwidth = 82\n",
    "plt.rc(\"figure\", figsize=(10, 6))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:46.707010Z",
     "end_time": "2024-04-18T23:03:47.719622Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 8.1 层次化索引"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "a  1   -0.204708\n   2    0.478943\n   3   -0.519439\nb  1   -0.555730\n   3    1.965781\nc  1    1.393406\n   2    0.092908\nd  2    0.281746\n   3    0.769023\ndtype: float64"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.Series(np.random.randn(9), index=[['a', 'a', 'a', 'b', 'b', 'c', 'c', 'd', 'd'], [1, 2, 3, 1, 3, 1, 2, 2, 3]])\n",
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:47.627731Z",
     "end_time": "2024-04-18T23:03:47.719622Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "MultiIndex([('a', 1),\n            ('a', 2),\n            ('a', 3),\n            ('b', 1),\n            ('b', 3),\n            ('c', 1),\n            ('c', 2),\n            ('d', 2),\n            ('d', 3)],\n           )"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:47.648049Z",
     "end_time": "2024-04-18T23:03:47.719622Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "1   -0.555730\n3    1.965781\ndtype: float64"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['b']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:47.671055Z",
     "end_time": "2024-04-18T23:03:47.719622Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "b  1   -0.555730\n   3    1.965781\nc  1    1.393406\n   2    0.092908\ndtype: float64"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['b':'c']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:47.691741Z",
     "end_time": "2024-04-18T23:03:47.719622Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "b  1   -0.555730\n   3    1.965781\nd  2    0.281746\n   3    0.769023\ndtype: float64"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.loc[['b', 'd']]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:47.711347Z",
     "end_time": "2024-04-18T23:03:47.730192Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "a    0.478943\nc    0.092908\nd    0.281746\ndtype: float64"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.loc[:, 2]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:47.730689Z",
     "end_time": "2024-04-18T23:03:47.880712Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "          1         2         3\na -0.204708  0.478943 -0.519439\nb -0.555730       NaN  1.965781\nc  1.393406  0.092908       NaN\nd       NaN  0.281746  0.769023",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>1</th>\n      <th>2</th>\n      <th>3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>-0.204708</td>\n      <td>0.478943</td>\n      <td>-0.519439</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>-0.555730</td>\n      <td>NaN</td>\n      <td>1.965781</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>1.393406</td>\n      <td>0.092908</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>NaN</td>\n      <td>0.281746</td>\n      <td>0.769023</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.unstack()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:47.752579Z",
     "end_time": "2024-04-18T23:03:47.880712Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "a  1   -0.204708\n   2    0.478943\n   3   -0.519439\nb  1   -0.555730\n   3    1.965781\nc  1    1.393406\n   2    0.092908\nd  2    0.281746\n   3    0.769023\ndtype: float64"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.unstack().stack()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:47.771681Z",
     "end_time": "2024-04-18T23:03:47.880712Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "     Ohio     Colorado\n    Green Red    Green\na 1     0   1        2\n  2     3   4        5\nb 1     6   7        8\n  2     9  10       11",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th></th>\n      <th colspan=\"2\" halign=\"left\">Ohio</th>\n      <th>Colorado</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th></th>\n      <th>Green</th>\n      <th>Red</th>\n      <th>Green</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">a</th>\n      <th>1</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">b</th>\n      <th>1</th>\n      <td>6</td>\n      <td>7</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame = pd.DataFrame(np.arange(12).reshape((4, 3)), index=[['a', 'a', 'b', 'b'], [1, 2, 1, 2]],\n",
    "                     columns=[['Ohio', 'Ohio', 'Colorado'], ['Green', 'Red', 'Green']])\n",
    "frame"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:47.825469Z",
     "end_time": "2024-04-18T23:03:47.880712Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "state      Ohio     Colorado\ncolor     Green Red    Green\nkey1 key2                   \na    1        0   1        2\n     2        3   4        5\nb    1        6   7        8\n     2        9  10       11",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th>state</th>\n      <th colspan=\"2\" halign=\"left\">Ohio</th>\n      <th>Colorado</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>color</th>\n      <th>Green</th>\n      <th>Red</th>\n      <th>Green</th>\n    </tr>\n    <tr>\n      <th>key1</th>\n      <th>key2</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">a</th>\n      <th>1</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">b</th>\n      <th>1</th>\n      <td>6</td>\n      <td>7</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.index.names = ['key1', 'key2']\n",
    "frame.columns.names = ['state', 'color']\n",
    "frame"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:47.839361Z",
     "end_time": "2024-04-18T23:03:47.880712Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "color      Green  Red\nkey1 key2            \na    1         0    1\n     2         3    4\nb    1         6    7\n     2         9   10",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>color</th>\n      <th>Green</th>\n      <th>Red</th>\n    </tr>\n    <tr>\n      <th>key1</th>\n      <th>key2</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">a</th>\n      <th>1</th>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">b</th>\n      <th>1</th>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>9</td>\n      <td>10</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame['Ohio']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:47.870880Z",
     "end_time": "2024-04-18T23:03:47.899452Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 重排与分级排序"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "state      Ohio     Colorado\ncolor     Green Red    Green\nkey2 key1                   \n1    a        0   1        2\n2    a        3   4        5\n1    b        6   7        8\n2    b        9  10       11",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th>state</th>\n      <th colspan=\"2\" halign=\"left\">Ohio</th>\n      <th>Colorado</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>color</th>\n      <th>Green</th>\n      <th>Red</th>\n      <th>Green</th>\n    </tr>\n    <tr>\n      <th>key2</th>\n      <th>key1</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <th>a</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <th>a</th>\n      <td>3</td>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <th>b</th>\n      <td>6</td>\n      <td>7</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <th>b</th>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.swaplevel('key1', 'key2')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:47.899452Z",
     "end_time": "2024-04-18T23:03:47.988828Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "state      Ohio     Colorado\ncolor     Green Red    Green\nkey1 key2                   \na    1        0   1        2\nb    1        6   7        8\na    2        3   4        5\nb    2        9  10       11",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th>state</th>\n      <th colspan=\"2\" halign=\"left\">Ohio</th>\n      <th>Colorado</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>color</th>\n      <th>Green</th>\n      <th>Red</th>\n      <th>Green</th>\n    </tr>\n    <tr>\n      <th>key1</th>\n      <th>key2</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <th>1</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <th>1</th>\n      <td>6</td>\n      <td>7</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>a</th>\n      <th>2</th>\n      <td>3</td>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <th>2</th>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.sort_index(level=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:47.923632Z",
     "end_time": "2024-04-18T23:03:48.043671Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "state      Ohio     Colorado\ncolor     Green Red    Green\nkey2 key1                   \n1    a        0   1        2\n     b        6   7        8\n2    a        3   4        5\n     b        9  10       11",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th>state</th>\n      <th colspan=\"2\" halign=\"left\">Ohio</th>\n      <th>Colorado</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>color</th>\n      <th>Green</th>\n      <th>Red</th>\n      <th>Green</th>\n    </tr>\n    <tr>\n      <th>key2</th>\n      <th>key1</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">1</th>\n      <th>a</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>6</td>\n      <td>7</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">2</th>\n      <th>a</th>\n      <td>3</td>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.swaplevel(0, 1).sort_index(level=0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:47.959477Z",
     "end_time": "2024-04-18T23:03:48.043671Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 根据级别汇总统计"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "state  Ohio     Colorado\ncolor Green Red    Green\nkey2                    \n1         6   8       10\n2        12  14       16",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th>state</th>\n      <th colspan=\"2\" halign=\"left\">Ohio</th>\n      <th>Colorado</th>\n    </tr>\n    <tr>\n      <th>color</th>\n      <th>Green</th>\n      <th>Red</th>\n      <th>Green</th>\n    </tr>\n    <tr>\n      <th>key2</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>6</td>\n      <td>8</td>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>12</td>\n      <td>14</td>\n      <td>16</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.groupby(level='key2').sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:47.983959Z",
     "end_time": "2024-04-18T23:03:48.125224Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "color      Green  Red\nkey1 key2            \na    1         2    1\n     2         8    4\nb    1        14    7\n     2        20   10",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>color</th>\n      <th>Green</th>\n      <th>Red</th>\n    </tr>\n    <tr>\n      <th>key1</th>\n      <th>key2</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">a</th>\n      <th>1</th>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>8</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">b</th>\n      <th>1</th>\n      <td>14</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>20</td>\n      <td>10</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.T.groupby(level='color').sum().T"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.005449Z",
     "end_time": "2024-04-18T23:03:48.353941Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 使用DataFrame的列进行索引"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "   a  b    c  d\n0  0  7  one  0\n1  1  6  one  1\n2  2  5  one  2\n3  3  4  two  0\n4  4  3  two  1\n5  5  2  two  2\n6  6  1  two  3",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>7</td>\n      <td>one</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>6</td>\n      <td>one</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2</td>\n      <td>5</td>\n      <td>one</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>4</td>\n      <td>two</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>4</td>\n      <td>3</td>\n      <td>two</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>5</td>\n      <td>2</td>\n      <td>two</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>6</td>\n      <td>1</td>\n      <td>two</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame = pd.DataFrame({'a': range(7), 'b': range(7, 0, -1), 'c': ['one', 'one', 'one', 'two', 'two', 'two', 'two'],\n",
    "                      'd': [0, 1, 2, 0, 1, 2, 3]})\n",
    "frame"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.030991Z",
     "end_time": "2024-04-18T23:03:48.374876Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "       a  b\nc   d      \none 0  0  7\n    1  1  6\n    2  2  5\ntwo 0  3  4\n    1  4  3\n    2  5  2\n    3  6  1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n    </tr>\n    <tr>\n      <th>c</th>\n      <th>d</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">one</th>\n      <th>0</th>\n      <td>0</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th rowspan=\"4\" valign=\"top\">two</th>\n      <th>0</th>\n      <td>3</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>5</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>6</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame2 = frame.set_index(['c', 'd'])\n",
    "frame2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.063903Z",
     "end_time": "2024-04-18T23:03:48.374876Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "       a  b    c  d\nc   d              \none 0  0  7  one  0\n    1  1  6  one  1\n    2  2  5  one  2\ntwo 0  3  4  two  0\n    1  4  3  two  1\n    2  5  2  two  2\n    3  6  1  two  3",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n    </tr>\n    <tr>\n      <th>c</th>\n      <th>d</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">one</th>\n      <th>0</th>\n      <td>0</td>\n      <td>7</td>\n      <td>one</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>6</td>\n      <td>one</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2</td>\n      <td>5</td>\n      <td>one</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th rowspan=\"4\" valign=\"top\">two</th>\n      <th>0</th>\n      <td>3</td>\n      <td>4</td>\n      <td>two</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4</td>\n      <td>3</td>\n      <td>two</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>5</td>\n      <td>2</td>\n      <td>two</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>6</td>\n      <td>1</td>\n      <td>two</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.set_index(['c', 'd'], drop=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.079004Z",
     "end_time": "2024-04-18T23:03:48.374876Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "     c  d  a  b\n0  one  0  0  7\n1  one  1  1  6\n2  one  2  2  5\n3  two  0  3  4\n4  two  1  4  3\n5  two  2  5  2\n6  two  3  6  1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>c</th>\n      <th>d</th>\n      <th>a</th>\n      <th>b</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>one</td>\n      <td>0</td>\n      <td>0</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>one</td>\n      <td>1</td>\n      <td>1</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>one</td>\n      <td>2</td>\n      <td>2</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>two</td>\n      <td>0</td>\n      <td>3</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>two</td>\n      <td>1</td>\n      <td>4</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>two</td>\n      <td>2</td>\n      <td>5</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>two</td>\n      <td>3</td>\n      <td>6</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame2.reset_index()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.103310Z",
     "end_time": "2024-04-18T23:03:48.374876Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 8.2 合并数据集"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 数据库风格的DataFrame合并"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "  key  data1\n0   b      0\n1   b      1\n2   a      2\n3   c      3\n4   a      4\n5   a      5\n6   b      6",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>data1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>c</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>a</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>b</td>\n      <td>6</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'], 'data1': range(7)})\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.125719Z",
     "end_time": "2024-04-18T23:03:48.374876Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "  key  data2\n0   a      0\n1   b      1\n2   d      2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>a</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>d</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = pd.DataFrame({'key': ['a', 'b', 'd'], 'data2': range(3)})\n",
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.143180Z",
     "end_time": "2024-04-18T23:03:48.374876Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "  key  data1  data2\n0   b      0      1\n1   b      1      1\n2   b      6      1\n3   a      2      0\n4   a      4      0\n5   a      5      0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>data1</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>b</td>\n      <td>6</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>a</td>\n      <td>2</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>4</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>a</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df1, df2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.163825Z",
     "end_time": "2024-04-18T23:03:48.374876Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "  key  data1  data2\n0   b      0      1\n1   b      1      1\n2   b      6      1\n3   a      2      0\n4   a      4      0\n5   a      5      0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>data1</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>b</td>\n      <td>6</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>a</td>\n      <td>2</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>4</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>a</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df1, df2, on='key')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.180775Z",
     "end_time": "2024-04-18T23:03:48.374876Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "  lkey  data1\n0    b      0\n1    b      1\n2    a      2\n3    c      3\n4    a      4\n5    a      5\n6    b      6",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>lkey</th>\n      <th>data1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>c</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>a</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>b</td>\n      <td>6</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3 = pd.DataFrame({'lkey': ['b', 'b', 'a', 'c', 'a', 'a', 'b'], 'data1': range(7)})\n",
    "df3"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.206068Z",
     "end_time": "2024-04-18T23:03:48.374876Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "  rkey  data2\n0    a      0\n1    b      1\n2    d      2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>rkey</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>a</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>d</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4 = pd.DataFrame({'rkey': ['a', 'b', 'd'], 'data2': range(3)})\n",
    "df4"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.231142Z",
     "end_time": "2024-04-18T23:03:48.374876Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "data": {
      "text/plain": "  lkey  data1 rkey  data2\n0    b      0    b      1\n1    b      1    b      1\n2    b      6    b      1\n3    a      2    a      0\n4    a      4    a      0\n5    a      5    a      0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>lkey</th>\n      <th>data1</th>\n      <th>rkey</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>0</td>\n      <td>b</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>1</td>\n      <td>b</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>b</td>\n      <td>6</td>\n      <td>b</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>a</td>\n      <td>2</td>\n      <td>a</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>4</td>\n      <td>a</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>a</td>\n      <td>5</td>\n      <td>a</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df3, df4, left_on='lkey', right_on='rkey')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.246885Z",
     "end_time": "2024-04-18T23:03:48.541685Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "data": {
      "text/plain": "  key  data1  data2\n0   b    0.0    1.0\n1   b    1.0    1.0\n2   b    6.0    1.0\n3   a    2.0    0.0\n4   a    4.0    0.0\n5   a    5.0    0.0\n6   c    3.0    NaN\n7   d    NaN    2.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>data1</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>0.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>1.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>b</td>\n      <td>6.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>a</td>\n      <td>2.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>4.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>a</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>c</td>\n      <td>3.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>d</td>\n      <td>NaN</td>\n      <td>2.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df1, df2, how='outer')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.267537Z",
     "end_time": "2024-04-18T23:03:48.541685Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "data": {
      "text/plain": "  key  data1\n0   b      0\n1   b      1\n2   a      2\n3   c      3\n4   a      4\n5   b      5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>data1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>c</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>b</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'b'], 'data1': range(6)})\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.288035Z",
     "end_time": "2024-04-18T23:03:48.541685Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "  key  data2\n0   a      0\n1   b      1\n2   a      2\n3   b      3\n4   d      4",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>a</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>b</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>d</td>\n      <td>4</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = pd.DataFrame({'key': ['a', 'b', 'a', 'b', 'd'], 'data2': range(5)})\n",
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.316945Z",
     "end_time": "2024-04-18T23:03:48.541685Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "data": {
      "text/plain": "   key  data1  data2\n0    b      0    1.0\n1    b      0    3.0\n2    b      1    1.0\n3    b      1    3.0\n4    a      2    0.0\n5    a      2    2.0\n6    c      3    NaN\n7    a      4    0.0\n8    a      4    2.0\n9    b      5    1.0\n10   b      5    3.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>data1</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>0</td>\n      <td>3.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>b</td>\n      <td>1</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>b</td>\n      <td>1</td>\n      <td>3.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>2</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>a</td>\n      <td>2</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>c</td>\n      <td>3</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>a</td>\n      <td>4</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>a</td>\n      <td>4</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>b</td>\n      <td>5</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>b</td>\n      <td>5</td>\n      <td>3.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df1, df2, on='key', how='left')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.333311Z",
     "end_time": "2024-04-18T23:03:48.541685Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "data": {
      "text/plain": "  key  data1  data2\n0   b      0      1\n1   b      0      3\n2   b      1      1\n3   b      1      3\n4   b      5      1\n5   b      5      3\n6   a      2      0\n7   a      2      2\n8   a      4      0\n9   a      4      2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>data1</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>0</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>b</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>b</td>\n      <td>1</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>b</td>\n      <td>5</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>b</td>\n      <td>5</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>a</td>\n      <td>2</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>a</td>\n      <td>2</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>a</td>\n      <td>4</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>a</td>\n      <td>4</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df1, df2, how='inner')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.357486Z",
     "end_time": "2024-04-18T23:03:48.541685Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "data": {
      "text/plain": "  key1 key2  lval\n0  foo  one     1\n1  foo  two     2\n2  bar  one     3",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>key2</th>\n      <th>lval</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>foo</td>\n      <td>one</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>foo</td>\n      <td>two</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>bar</td>\n      <td>one</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left = pd.DataFrame({'key1': ['foo', 'foo', 'bar'], 'key2': ['one', 'two', 'one'], 'lval': [1, 2, 3]})\n",
    "left"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.374876Z",
     "end_time": "2024-04-18T23:03:48.675546Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "data": {
      "text/plain": "  key1 key2  rval\n0  foo  one     4\n1  foo  one     5\n2  bar  one     6\n3  bar  two     7",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>key2</th>\n      <th>rval</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>foo</td>\n      <td>one</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>foo</td>\n      <td>one</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>bar</td>\n      <td>one</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>bar</td>\n      <td>two</td>\n      <td>7</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "right = pd.DataFrame({'key1': ['foo', 'foo', 'bar', 'bar'], 'key2': ['one', 'one', 'one', 'two'], 'rval': [4, 5, 6, 7]})\n",
    "right"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.391573Z",
     "end_time": "2024-04-18T23:03:48.741472Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "data": {
      "text/plain": "  key1 key2  lval  rval\n0  foo  one   1.0   4.0\n1  foo  one   1.0   5.0\n2  foo  two   2.0   NaN\n3  bar  one   3.0   6.0\n4  bar  two   NaN   7.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>key2</th>\n      <th>lval</th>\n      <th>rval</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>foo</td>\n      <td>one</td>\n      <td>1.0</td>\n      <td>4.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>foo</td>\n      <td>one</td>\n      <td>1.0</td>\n      <td>5.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>foo</td>\n      <td>two</td>\n      <td>2.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>bar</td>\n      <td>one</td>\n      <td>3.0</td>\n      <td>6.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>bar</td>\n      <td>two</td>\n      <td>NaN</td>\n      <td>7.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(left, right, on=['key1', 'key2'], how='outer')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.415904Z",
     "end_time": "2024-04-18T23:03:48.804552Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "data": {
      "text/plain": "  key1 key2_x  lval key2_y  rval\n0  foo    one     1    one     4\n1  foo    one     1    one     5\n2  foo    two     2    one     4\n3  foo    two     2    one     5\n4  bar    one     3    one     6\n5  bar    one     3    two     7",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>key2_x</th>\n      <th>lval</th>\n      <th>key2_y</th>\n      <th>rval</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>foo</td>\n      <td>one</td>\n      <td>1</td>\n      <td>one</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>foo</td>\n      <td>one</td>\n      <td>1</td>\n      <td>one</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>foo</td>\n      <td>two</td>\n      <td>2</td>\n      <td>one</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>foo</td>\n      <td>two</td>\n      <td>2</td>\n      <td>one</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>bar</td>\n      <td>one</td>\n      <td>3</td>\n      <td>one</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>bar</td>\n      <td>one</td>\n      <td>3</td>\n      <td>two</td>\n      <td>7</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(left, right, on='key1')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.438584Z",
     "end_time": "2024-04-18T23:03:48.856492Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "data": {
      "text/plain": "  key1 key2_left  lval key2_right  rval\n0  foo       one     1        one     4\n1  foo       one     1        one     5\n2  foo       two     2        one     4\n3  foo       two     2        one     5\n4  bar       one     3        one     6\n5  bar       one     3        two     7",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>key2_left</th>\n      <th>lval</th>\n      <th>key2_right</th>\n      <th>rval</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>foo</td>\n      <td>one</td>\n      <td>1</td>\n      <td>one</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>foo</td>\n      <td>one</td>\n      <td>1</td>\n      <td>one</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>foo</td>\n      <td>two</td>\n      <td>2</td>\n      <td>one</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>foo</td>\n      <td>two</td>\n      <td>2</td>\n      <td>one</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>bar</td>\n      <td>one</td>\n      <td>3</td>\n      <td>one</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>bar</td>\n      <td>one</td>\n      <td>3</td>\n      <td>two</td>\n      <td>7</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(left, right, on='key1', suffixes=('_left', '_right'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.467167Z",
     "end_time": "2024-04-18T23:03:48.856492Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 索引上的合并"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "data": {
      "text/plain": "  key  value\n0   a      0\n1   b      1\n2   a      2\n3   a      3\n4   b      4\n5   c      5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>value</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>a</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>a</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>b</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>c</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left1 = pd.DataFrame({'key': ['a', 'b', 'a', 'a', 'b', 'c'], 'value': range(6)})\n",
    "left1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.483583Z",
     "end_time": "2024-04-18T23:03:48.921897Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "data": {
      "text/plain": "   group_val\na        3.5\nb        7.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>group_val</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>3.5</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>7.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "right1 = pd.DataFrame({'group_val': [3.5, 7]}, index=['a', 'b'])\n",
    "right1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.531200Z",
     "end_time": "2024-04-18T23:03:48.921897Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [
    {
     "data": {
      "text/plain": "  key  value  group_val\n0   a      0        3.5\n2   a      2        3.5\n3   a      3        3.5\n1   b      1        7.0\n4   b      4        7.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>value</th>\n      <th>group_val</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>a</td>\n      <td>0</td>\n      <td>3.5</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>2</td>\n      <td>3.5</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>a</td>\n      <td>3</td>\n      <td>3.5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>1</td>\n      <td>7.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>b</td>\n      <td>4</td>\n      <td>7.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(left1, right1, left_on='key', right_index=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.552340Z",
     "end_time": "2024-04-18T23:03:48.921897Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [
    {
     "data": {
      "text/plain": "  key  value  group_val\n0   a      0        3.5\n2   a      2        3.5\n3   a      3        3.5\n1   b      1        7.0\n4   b      4        7.0\n5   c      5        NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>value</th>\n      <th>group_val</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>a</td>\n      <td>0</td>\n      <td>3.5</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>2</td>\n      <td>3.5</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>a</td>\n      <td>3</td>\n      <td>3.5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>1</td>\n      <td>7.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>b</td>\n      <td>4</td>\n      <td>7.0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>c</td>\n      <td>5</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(left1, right1, left_on='key', right_index=True, how='outer')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.577309Z",
     "end_time": "2024-04-18T23:03:48.921897Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "data": {
      "text/plain": "     key1  key2  data\n0    Ohio  2000   0.0\n1    Ohio  2001   1.0\n2    Ohio  2002   2.0\n3  Nevada  2001   3.0\n4  Nevada  2002   4.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>key2</th>\n      <th>data</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>Ohio</td>\n      <td>2000</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>Ohio</td>\n      <td>2001</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>Ohio</td>\n      <td>2002</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>Nevada</td>\n      <td>2001</td>\n      <td>3.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>Nevada</td>\n      <td>2002</td>\n      <td>4.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lefth = pd.DataFrame({'key1': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'], 'key2': [2000, 2001, 2002, 2001, 2002],\n",
    "                      'data': np.arange(5.)})\n",
    "lefth"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.601847Z",
     "end_time": "2024-04-18T23:03:49.000304Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "outputs": [
    {
     "data": {
      "text/plain": "             event1  event2\nNevada 2001       0       1\n       2000       2       3\nOhio   2000       4       5\n       2000       6       7\n       2001       8       9\n       2002      10      11",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>event1</th>\n      <th>event2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">Nevada</th>\n      <th>2001</th>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2000</th>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th rowspan=\"4\" valign=\"top\">Ohio</th>\n      <th>2000</th>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>2000</th>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>2001</th>\n      <td>8</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>2002</th>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "righth = pd.DataFrame(np.arange(12).reshape((6, 2)), index=[['Nevada', 'Nevada', 'Ohio', 'Ohio', 'Ohio', 'Ohio'],\n",
    "                                                            [2001, 2000, 2000, 2000, 2001, 2002]],\n",
    "                      columns=['event1', 'event2'])\n",
    "righth"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.622194Z",
     "end_time": "2024-04-18T23:03:49.151332Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "outputs": [
    {
     "data": {
      "text/plain": "     key1  key2  data  event1  event2\n0    Ohio  2000   0.0       4       5\n0    Ohio  2000   0.0       6       7\n1    Ohio  2001   1.0       8       9\n2    Ohio  2002   2.0      10      11\n3  Nevada  2001   3.0       0       1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>key2</th>\n      <th>data</th>\n      <th>event1</th>\n      <th>event2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>Ohio</td>\n      <td>2000</td>\n      <td>0.0</td>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>0</th>\n      <td>Ohio</td>\n      <td>2000</td>\n      <td>0.0</td>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>Ohio</td>\n      <td>2001</td>\n      <td>1.0</td>\n      <td>8</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>Ohio</td>\n      <td>2002</td>\n      <td>2.0</td>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>Nevada</td>\n      <td>2001</td>\n      <td>3.0</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(lefth, righth, left_on=['key1', 'key2'], right_index=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.641239Z",
     "end_time": "2024-04-18T23:03:49.306168Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "outputs": [
    {
     "data": {
      "text/plain": "     key1  key2  data  event1  event2\n0    Ohio  2000   0.0     4.0     5.0\n0    Ohio  2000   0.0     6.0     7.0\n1    Ohio  2001   1.0     8.0     9.0\n2    Ohio  2002   2.0    10.0    11.0\n3  Nevada  2001   3.0     0.0     1.0\n4  Nevada  2002   4.0     NaN     NaN\n4  Nevada  2000   NaN     2.0     3.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>key2</th>\n      <th>data</th>\n      <th>event1</th>\n      <th>event2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>Ohio</td>\n      <td>2000</td>\n      <td>0.0</td>\n      <td>4.0</td>\n      <td>5.0</td>\n    </tr>\n    <tr>\n      <th>0</th>\n      <td>Ohio</td>\n      <td>2000</td>\n      <td>0.0</td>\n      <td>6.0</td>\n      <td>7.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>Ohio</td>\n      <td>2001</td>\n      <td>1.0</td>\n      <td>8.0</td>\n      <td>9.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>Ohio</td>\n      <td>2002</td>\n      <td>2.0</td>\n      <td>10.0</td>\n      <td>11.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>Nevada</td>\n      <td>2001</td>\n      <td>3.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>Nevada</td>\n      <td>2002</td>\n      <td>4.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>Nevada</td>\n      <td>2000</td>\n      <td>NaN</td>\n      <td>2.0</td>\n      <td>3.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(lefth, righth, left_on=['key1', 'key2'], right_index=True, how='outer')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.658847Z",
     "end_time": "2024-04-18T23:03:49.306168Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "data": {
      "text/plain": "   Ohio  Nevada\na   1.0     2.0\nc   3.0     4.0\ne   5.0     6.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Ohio</th>\n      <th>Nevada</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1.0</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>3.0</td>\n      <td>4.0</td>\n    </tr>\n    <tr>\n      <th>e</th>\n      <td>5.0</td>\n      <td>6.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left2 = pd.DataFrame([[1., 2.], [3., 4.], [5., 6.]], index=['a', 'c', 'e'], columns=['Ohio', 'Nevada'])\n",
    "left2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.675546Z",
     "end_time": "2024-04-18T23:03:49.306168Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "data": {
      "text/plain": "   Missouri  Alabama\nb       7.0      8.0\nc       9.0     10.0\nd      11.0     12.0\ne      13.0     14.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Missouri</th>\n      <th>Alabama</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>b</th>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>9.0</td>\n      <td>10.0</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>11.0</td>\n      <td>12.0</td>\n    </tr>\n    <tr>\n      <th>e</th>\n      <td>13.0</td>\n      <td>14.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "right2 = pd.DataFrame([[7., 8.], [9., 10.], [11., 12.], [13., 14.]], index=['b', 'c', 'd', 'e'],\n",
    "                      columns=['Missouri', 'Alabama'])\n",
    "right2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.692065Z",
     "end_time": "2024-04-18T23:03:49.401467Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "outputs": [
    {
     "data": {
      "text/plain": "   Ohio  Nevada  Missouri  Alabama\na   1.0     2.0       NaN      NaN\nb   NaN     NaN       7.0      8.0\nc   3.0     4.0       9.0     10.0\nd   NaN     NaN      11.0     12.0\ne   5.0     6.0      13.0     14.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Ohio</th>\n      <th>Nevada</th>\n      <th>Missouri</th>\n      <th>Alabama</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>3.0</td>\n      <td>4.0</td>\n      <td>9.0</td>\n      <td>10.0</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>11.0</td>\n      <td>12.0</td>\n    </tr>\n    <tr>\n      <th>e</th>\n      <td>5.0</td>\n      <td>6.0</td>\n      <td>13.0</td>\n      <td>14.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(left2, right2, how='outer', left_index=True, right_index=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.721588Z",
     "end_time": "2024-04-18T23:03:49.447647Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [
    {
     "data": {
      "text/plain": "   Ohio  Nevada  Missouri  Alabama\na   1.0     2.0       NaN      NaN\nb   NaN     NaN       7.0      8.0\nc   3.0     4.0       9.0     10.0\nd   NaN     NaN      11.0     12.0\ne   5.0     6.0      13.0     14.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Ohio</th>\n      <th>Nevada</th>\n      <th>Missouri</th>\n      <th>Alabama</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>3.0</td>\n      <td>4.0</td>\n      <td>9.0</td>\n      <td>10.0</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>11.0</td>\n      <td>12.0</td>\n    </tr>\n    <tr>\n      <th>e</th>\n      <td>5.0</td>\n      <td>6.0</td>\n      <td>13.0</td>\n      <td>14.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left2.join(right2, how='outer')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.743652Z",
     "end_time": "2024-04-18T23:03:49.447647Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "outputs": [
    {
     "data": {
      "text/plain": "  key  value  group_val\n0   a      0        3.5\n1   b      1        7.0\n2   a      2        3.5\n3   a      3        3.5\n4   b      4        7.0\n5   c      5        NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>value</th>\n      <th>group_val</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>a</td>\n      <td>0</td>\n      <td>3.5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>1</td>\n      <td>7.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>2</td>\n      <td>3.5</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>a</td>\n      <td>3</td>\n      <td>3.5</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>b</td>\n      <td>4</td>\n      <td>7.0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>c</td>\n      <td>5</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left1.join(right1, on='key')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.762908Z",
     "end_time": "2024-04-18T23:03:49.447647Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "outputs": [
    {
     "data": {
      "text/plain": "   New York  Oregon\na       7.0     8.0\nc       9.0    10.0\ne      11.0    12.0\nf      16.0    17.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>New York</th>\n      <th>Oregon</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>9.0</td>\n      <td>10.0</td>\n    </tr>\n    <tr>\n      <th>e</th>\n      <td>11.0</td>\n      <td>12.0</td>\n    </tr>\n    <tr>\n      <th>f</th>\n      <td>16.0</td>\n      <td>17.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "another = pd.DataFrame([[7., 8.], [9., 10.], [11., 12.], [16., 17.]], index=['a', 'c', 'e', 'f'],\n",
    "                       columns=['New York', 'Oregon'])\n",
    "another"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.783904Z",
     "end_time": "2024-04-18T23:03:49.447647Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "outputs": [
    {
     "data": {
      "text/plain": "   Ohio  Nevada  Missouri  Alabama  New York  Oregon\na   1.0     2.0       NaN      NaN       7.0     8.0\nc   3.0     4.0       9.0     10.0       9.0    10.0\ne   5.0     6.0      13.0     14.0      11.0    12.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Ohio</th>\n      <th>Nevada</th>\n      <th>Missouri</th>\n      <th>Alabama</th>\n      <th>New York</th>\n      <th>Oregon</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>3.0</td>\n      <td>4.0</td>\n      <td>9.0</td>\n      <td>10.0</td>\n      <td>9.0</td>\n      <td>10.0</td>\n    </tr>\n    <tr>\n      <th>e</th>\n      <td>5.0</td>\n      <td>6.0</td>\n      <td>13.0</td>\n      <td>14.0</td>\n      <td>11.0</td>\n      <td>12.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left2.join([right2, another])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.804056Z",
     "end_time": "2024-04-18T23:03:49.448144Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "outputs": [
    {
     "data": {
      "text/plain": "   Ohio  Nevada  Missouri  Alabama  New York  Oregon\na   1.0     2.0       NaN      NaN       7.0     8.0\nc   3.0     4.0       9.0     10.0       9.0    10.0\ne   5.0     6.0      13.0     14.0      11.0    12.0\nb   NaN     NaN       7.0      8.0       NaN     NaN\nd   NaN     NaN      11.0     12.0       NaN     NaN\nf   NaN     NaN       NaN      NaN      16.0    17.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Ohio</th>\n      <th>Nevada</th>\n      <th>Missouri</th>\n      <th>Alabama</th>\n      <th>New York</th>\n      <th>Oregon</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>3.0</td>\n      <td>4.0</td>\n      <td>9.0</td>\n      <td>10.0</td>\n      <td>9.0</td>\n      <td>10.0</td>\n    </tr>\n    <tr>\n      <th>e</th>\n      <td>5.0</td>\n      <td>6.0</td>\n      <td>13.0</td>\n      <td>14.0</td>\n      <td>11.0</td>\n      <td>12.0</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>7.0</td>\n      <td>8.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>11.0</td>\n      <td>12.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>f</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>16.0</td>\n      <td>17.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left2.join([right2, another], how='outer')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.835685Z",
     "end_time": "2024-04-18T23:03:49.448144Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 轴向连接"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 0,  1,  2,  3],\n       [ 4,  5,  6,  7],\n       [ 8,  9, 10, 11]])"
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(12).reshape((3, 4))\n",
    "arr"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.855236Z",
     "end_time": "2024-04-18T23:03:49.448144Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 0,  1,  2,  3,  0,  1,  2,  3],\n       [ 4,  5,  6,  7,  4,  5,  6,  7],\n       [ 8,  9, 10, 11,  8,  9, 10, 11]])"
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([arr, arr], axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.874807Z",
     "end_time": "2024-04-18T23:03:49.448144Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "outputs": [
    {
     "data": {
      "text/plain": "a    0\nb    1\ndtype: int64"
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = pd.Series([0, 1], index=['a', 'b'])\n",
    "s1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.899992Z",
     "end_time": "2024-04-18T23:03:49.448144Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "outputs": [
    {
     "data": {
      "text/plain": "c    2\nd    3\ne    4\ndtype: int64"
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2 = pd.Series([2, 3, 4], index=['c', 'd', 'e'])\n",
    "s2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.916195Z",
     "end_time": "2024-04-18T23:03:49.448144Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "outputs": [
    {
     "data": {
      "text/plain": "f    5\ng    6\ndtype: int64"
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s3 = pd.Series([5, 6], index=['f', 'g'])\n",
    "s3"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.938202Z",
     "end_time": "2024-04-18T23:03:49.467870Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "outputs": [
    {
     "data": {
      "text/plain": "a    0\nb    1\nc    2\nd    3\ne    4\nf    5\ng    6\ndtype: int64"
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([s1, s2, s3])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.954887Z",
     "end_time": "2024-04-18T23:03:49.517206Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "outputs": [
    {
     "data": {
      "text/plain": "     0    1    2\na  0.0  NaN  NaN\nb  1.0  NaN  NaN\nc  NaN  2.0  NaN\nd  NaN  3.0  NaN\ne  NaN  4.0  NaN\nf  NaN  NaN  5.0\ng  NaN  NaN  6.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>1.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>NaN</td>\n      <td>2.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>NaN</td>\n      <td>3.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>e</th>\n      <td>NaN</td>\n      <td>4.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>f</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>5.0</td>\n    </tr>\n    <tr>\n      <th>g</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>6.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([s1, s2, s3], axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.971487Z",
     "end_time": "2024-04-18T23:03:49.573363Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "outputs": [
    {
     "data": {
      "text/plain": "a    0\nb    1\nf    5\ng    6\ndtype: int64"
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s4 = pd.concat([s1, s3])\n",
    "s4"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:48.991797Z",
     "end_time": "2024-04-18T23:03:49.627275Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "outputs": [
    {
     "data": {
      "text/plain": "     0  1\na  0.0  0\nb  1.0  1\nf  NaN  5\ng  NaN  6",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>1.0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>f</th>\n      <td>NaN</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>g</th>\n      <td>NaN</td>\n      <td>6</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([s1, s4], axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.017039Z",
     "end_time": "2024-04-18T23:03:49.727755Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "outputs": [
    {
     "data": {
      "text/plain": "   0  1\na  0  0\nb  1  1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([s1, s4], axis=1, join='inner')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.042208Z",
     "end_time": "2024-04-18T23:03:49.727755Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "outputs": [
    {
     "data": {
      "text/plain": "one    a    0\n       b    1\ntwo    a    0\n       b    1\nthree  f    5\n       g    6\ndtype: int64"
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = pd.concat([s1, s1, s3], keys=['one', 'two', 'three'])\n",
    "result"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.058309Z",
     "end_time": "2024-04-18T23:03:49.751968Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "outputs": [
    {
     "data": {
      "text/plain": "         a    b    f    g\none    0.0  1.0  NaN  NaN\ntwo    0.0  1.0  NaN  NaN\nthree  NaN  NaN  5.0  6.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>f</th>\n      <th>g</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>one</th>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>two</th>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>three</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>5.0</td>\n      <td>6.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.unstack()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.076036Z",
     "end_time": "2024-04-18T23:03:49.754297Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "outputs": [
    {
     "data": {
      "text/plain": "   one  two  three\na  0.0  NaN    NaN\nb  1.0  NaN    NaN\nc  NaN  2.0    NaN\nd  NaN  3.0    NaN\ne  NaN  4.0    NaN\nf  NaN  NaN    5.0\ng  NaN  NaN    6.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n      <th>three</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>1.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>NaN</td>\n      <td>2.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>NaN</td>\n      <td>3.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>e</th>\n      <td>NaN</td>\n      <td>4.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>f</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>5.0</td>\n    </tr>\n    <tr>\n      <th>g</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>6.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([s1, s2, s3], axis=1, keys=['one', 'two', 'three'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.092851Z",
     "end_time": "2024-04-18T23:03:49.765021Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "outputs": [
    {
     "data": {
      "text/plain": "   one  two\na    0    1\nb    2    3\nc    4    5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pd.DataFrame(np.arange(6).reshape(3, 2), index=['a', 'b', 'c'],\n",
    "                   columns=['one', 'two'])\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.129419Z",
     "end_time": "2024-04-18T23:03:49.787268Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "outputs": [
    {
     "data": {
      "text/plain": "   three  four\na      5     6\nc      7     8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>three</th>\n      <th>four</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>5</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>7</td>\n      <td>8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = pd.DataFrame(5 + np.arange(4).reshape(2, 2), index=['a', 'c'],\n",
    "                   columns=['three', 'four'])\n",
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.157023Z",
     "end_time": "2024-04-18T23:03:49.805926Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "outputs": [
    {
     "data": {
      "text/plain": "  level1     level2     \n     one two  three four\na      0   1    5.0  6.0\nb      2   3    NaN  NaN\nc      4   5    7.0  8.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th colspan=\"2\" halign=\"left\">level1</th>\n      <th colspan=\"2\" halign=\"left\">level2</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n      <th>three</th>\n      <th>four</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0</td>\n      <td>1</td>\n      <td>5.0</td>\n      <td>6.0</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>2</td>\n      <td>3</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>4</td>\n      <td>5</td>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([df1, df2], axis=1, keys=['level1', 'level2'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.178150Z",
     "end_time": "2024-04-18T23:03:49.805926Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "outputs": [
    {
     "data": {
      "text/plain": "  level1     level2     \n     one two  three four\na      0   1    5.0  6.0\nb      2   3    NaN  NaN\nc      4   5    7.0  8.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th colspan=\"2\" halign=\"left\">level1</th>\n      <th colspan=\"2\" halign=\"left\">level2</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n      <th>three</th>\n      <th>four</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0</td>\n      <td>1</td>\n      <td>5.0</td>\n      <td>6.0</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>2</td>\n      <td>3</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>4</td>\n      <td>5</td>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat({'level1': df1, 'level2': df2}, axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.198723Z",
     "end_time": "2024-04-18T23:03:49.834884Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "outputs": [
    {
     "data": {
      "text/plain": "upper level1     level2     \nlower    one two  three four\na          0   1    5.0  6.0\nb          2   3    NaN  NaN\nc          4   5    7.0  8.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th>upper</th>\n      <th colspan=\"2\" halign=\"left\">level1</th>\n      <th colspan=\"2\" halign=\"left\">level2</th>\n    </tr>\n    <tr>\n      <th>lower</th>\n      <th>one</th>\n      <th>two</th>\n      <th>three</th>\n      <th>four</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0</td>\n      <td>1</td>\n      <td>5.0</td>\n      <td>6.0</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>2</td>\n      <td>3</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>4</td>\n      <td>5</td>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([df1, df2], axis=1, keys=['level1', 'level2'],\n",
    "          names=['upper', 'lower'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.217196Z",
     "end_time": "2024-04-18T23:03:49.845556Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "outputs": [
    {
     "data": {
      "text/plain": "          a         b         c         d\n0  1.246435  1.007189 -1.296221  0.274992\n1  0.228913  1.352917  0.886429 -2.001637\n2 -0.371843  1.669025 -0.438570 -0.539741",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1.246435</td>\n      <td>1.007189</td>\n      <td>-1.296221</td>\n      <td>0.274992</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.228913</td>\n      <td>1.352917</td>\n      <td>0.886429</td>\n      <td>-2.001637</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>-0.371843</td>\n      <td>1.669025</td>\n      <td>-0.438570</td>\n      <td>-0.539741</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pd.DataFrame(np.random.randn(3, 4), columns=['a', 'b', 'c', 'd'])\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.239547Z",
     "end_time": "2024-04-18T23:03:49.863821Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "outputs": [
    {
     "data": {
      "text/plain": "          b         d         a\n0  0.476985  3.248944 -1.021228\n1 -0.577087  0.124121  0.302614",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>b</th>\n      <th>d</th>\n      <th>a</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.476985</td>\n      <td>3.248944</td>\n      <td>-1.021228</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>-0.577087</td>\n      <td>0.124121</td>\n      <td>0.302614</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = pd.DataFrame(np.random.randn(2, 3), columns=['b', 'd', 'a'])\n",
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.259740Z",
     "end_time": "2024-04-18T23:03:49.868564Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "outputs": [
    {
     "data": {
      "text/plain": "          a         b         c         d\n0  1.246435  1.007189 -1.296221  0.274992\n1  0.228913  1.352917  0.886429 -2.001637\n2 -0.371843  1.669025 -0.438570 -0.539741\n3 -1.021228  0.476985       NaN  3.248944\n4  0.302614 -0.577087       NaN  0.124121",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1.246435</td>\n      <td>1.007189</td>\n      <td>-1.296221</td>\n      <td>0.274992</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.228913</td>\n      <td>1.352917</td>\n      <td>0.886429</td>\n      <td>-2.001637</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>-0.371843</td>\n      <td>1.669025</td>\n      <td>-0.438570</td>\n      <td>-0.539741</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>-1.021228</td>\n      <td>0.476985</td>\n      <td>NaN</td>\n      <td>3.248944</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0.302614</td>\n      <td>-0.577087</td>\n      <td>NaN</td>\n      <td>0.124121</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([df1, df2], ignore_index=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.276440Z",
     "end_time": "2024-04-18T23:03:49.891140Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 合并重叠数据"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "outputs": [
    {
     "data": {
      "text/plain": "f    NaN\ne    2.5\nd    NaN\nc    3.5\nb    4.5\na    NaN\ndtype: float64"
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = pd.Series([NA, 2.5, NA, 3.5, 4.5, NA], index=['f', 'e', 'd', 'c', 'b', 'a'])\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.306168Z",
     "end_time": "2024-04-18T23:03:50.014142Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\58248\\AppData\\Local\\Temp\\ipykernel_1380\\2356869643.py:2: FutureWarning: Series.__setitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To set a value by position, use `ser.iloc[pos] = value`\n",
      "  b[-1] = NA\n"
     ]
    },
    {
     "data": {
      "text/plain": "f    0.0\ne    1.0\nd    2.0\nc    3.0\nb    4.0\na    NaN\ndtype: float64"
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = pd.Series(np.arange(len(a), dtype=np.float64), index=['f', 'e', 'd', 'c', 'b', 'a'])\n",
    "b[-1] = NA\n",
    "b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.322868Z",
     "end_time": "2024-04-18T23:03:50.171846Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "outputs": [
    {
     "data": {
      "text/plain": "array([0. , 2.5, 2. , 3.5, 4.5, nan])"
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(pd.isnull(a), b, a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.348429Z",
     "end_time": "2024-04-18T23:03:50.377251Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "outputs": [
    {
     "data": {
      "text/plain": "a    NaN\nb    4.5\nc    3.0\nd    2.0\ne    1.0\nf    0.0\ndtype: float64"
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[:-2].combine_first(a[2:])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.369502Z",
     "end_time": "2024-04-18T23:03:50.378243Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "outputs": [
    {
     "data": {
      "text/plain": "     a    b   c\n0  1.0  NaN   2\n1  NaN  2.0   6\n2  5.0  NaN  10\n3  NaN  6.0  14",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1.0</td>\n      <td>NaN</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>NaN</td>\n      <td>2.0</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>5.0</td>\n      <td>NaN</td>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>NaN</td>\n      <td>6.0</td>\n      <td>14</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pd.DataFrame({'a': [1, NA, 5, NA], 'b': [NA, 2., NA, 6.], 'c': range(2, 18, 4)})\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.386295Z",
     "end_time": "2024-04-18T23:03:50.378243Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "outputs": [
    {
     "data": {
      "text/plain": "     a    b\n0  5.0  NaN\n1  4.0  3.0\n2  NaN  4.0\n3  3.0  6.0\n4  7.0  8.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>5.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4.0</td>\n      <td>3.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>NaN</td>\n      <td>4.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3.0</td>\n      <td>6.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = pd.DataFrame({'a': [5., 4., NA, 3., 7.], 'b': [NA, 3., 4., 6., 8.]})\n",
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.403224Z",
     "end_time": "2024-04-18T23:03:50.378243Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "outputs": [
    {
     "data": {
      "text/plain": "     a    b     c\n0  1.0  NaN   2.0\n1  4.0  2.0   6.0\n2  5.0  4.0  10.0\n3  3.0  6.0  14.0\n4  7.0  8.0   NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1.0</td>\n      <td>NaN</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4.0</td>\n      <td>2.0</td>\n      <td>6.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>5.0</td>\n      <td>4.0</td>\n      <td>10.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3.0</td>\n      <td>6.0</td>\n      <td>14.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>7.0</td>\n      <td>8.0</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.combine_first(df2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.425949Z",
     "end_time": "2024-04-18T23:03:50.519960Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 8.3 重塑和轴向旋转"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 重塑层次化索引"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "outputs": [
    {
     "data": {
      "text/plain": "number    one  two  three\nstate                    \nOhio        0    1      2\nColorado    3    4      5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>number</th>\n      <th>one</th>\n      <th>two</th>\n      <th>three</th>\n    </tr>\n    <tr>\n      <th>state</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Ohio</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>Colorado</th>\n      <td>3</td>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.DataFrame(np.arange(6).reshape((2, 3)),\n",
    "                    index=pd.Index(['Ohio', 'Colorado'], name='state'),\n",
    "                    columns=pd.Index(['one', 'two', 'three'],\n",
    "                                     name='number'))\n",
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.442743Z",
     "end_time": "2024-04-18T23:03:50.535789Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "outputs": [
    {
     "data": {
      "text/plain": "state     number\nOhio      one       0\n          two       1\n          three     2\nColorado  one       3\n          two       4\n          three     5\ndtype: int32"
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = data.stack()\n",
    "result"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.468067Z",
     "end_time": "2024-04-18T23:03:50.602600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "outputs": [
    {
     "data": {
      "text/plain": "number    one  two  three\nstate                    \nOhio        0    1      2\nColorado    3    4      5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>number</th>\n      <th>one</th>\n      <th>two</th>\n      <th>three</th>\n    </tr>\n    <tr>\n      <th>state</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Ohio</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>Colorado</th>\n      <td>3</td>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.unstack()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.494105Z",
     "end_time": "2024-04-18T23:03:50.602600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "outputs": [
    {
     "data": {
      "text/plain": "state   Ohio  Colorado\nnumber                \none        0         3\ntwo        1         4\nthree      2         5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>state</th>\n      <th>Ohio</th>\n      <th>Colorado</th>\n    </tr>\n    <tr>\n      <th>number</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>one</th>\n      <td>0</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>two</th>\n      <td>1</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>three</th>\n      <td>2</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.unstack(0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.522537Z",
     "end_time": "2024-04-18T23:03:50.602600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "outputs": [
    {
     "data": {
      "text/plain": "state   Ohio  Colorado\nnumber                \none        0         3\ntwo        1         4\nthree      2         5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>state</th>\n      <th>Ohio</th>\n      <th>Colorado</th>\n    </tr>\n    <tr>\n      <th>number</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>one</th>\n      <td>0</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>two</th>\n      <td>1</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>three</th>\n      <td>2</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.unstack('state')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.542009Z",
     "end_time": "2024-04-18T23:03:50.602600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "outputs": [
    {
     "data": {
      "text/plain": "one  a    0\n     b    1\n     c    2\n     d    3\ntwo  c    4\n     d    5\n     e    6\ndtype: Int64"
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = pd.Series([0, 1, 2, 3], index=[\"a\", \"b\", \"c\", \"d\"], dtype=\"Int64\")\n",
    "s2 = pd.Series([4, 5, 6], index=[\"c\", \"d\", \"e\"], dtype=\"Int64\")\n",
    "data2 = pd.concat([s1, s2], keys=[\"one\", \"two\"])\n",
    "data2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.564985Z",
     "end_time": "2024-04-18T23:03:50.602600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "outputs": [
    {
     "data": {
      "text/plain": "        a     b  c  d     e\none     0     1  2  3  <NA>\ntwo  <NA>  <NA>  4  5     6",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>one</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n      <td>&lt;NA&gt;</td>\n    </tr>\n    <tr>\n      <th>two</th>\n      <td>&lt;NA&gt;</td>\n      <td>&lt;NA&gt;</td>\n      <td>4</td>\n      <td>5</td>\n      <td>6</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.unstack()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.588636Z",
     "end_time": "2024-04-18T23:03:50.602600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "outputs": [
    {
     "data": {
      "text/plain": "one  a    0\n     b    1\n     c    2\n     d    3\ntwo  c    4\n     d    5\n     e    6\ndtype: Int64"
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.unstack().stack()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.602160Z",
     "end_time": "2024-04-18T23:03:50.670594Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "outputs": [
    {
     "data": {
      "text/plain": "one  a       0\n     b       1\n     c       2\n     d       3\n     e    <NA>\ntwo  a    <NA>\n     b    <NA>\n     c       4\n     d       5\n     e       6\ndtype: Int64"
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.unstack().stack(dropna=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.631008Z",
     "end_time": "2024-04-18T23:03:50.670594Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "outputs": [
    {
     "data": {
      "text/plain": "side             left  right\nstate    number             \nOhio     one        0      5\n         two        1      6\n         three      2      7\nColorado one        3      8\n         two        4      9\n         three      5     10",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>side</th>\n      <th>left</th>\n      <th>right</th>\n    </tr>\n    <tr>\n      <th>state</th>\n      <th>number</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">Ohio</th>\n      <th>one</th>\n      <td>0</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>two</th>\n      <td>1</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>three</th>\n      <td>2</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">Colorado</th>\n      <th>one</th>\n      <td>3</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>two</th>\n      <td>4</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>three</th>\n      <td>5</td>\n      <td>10</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({\"left\": result, \"right\": result + 5},\n",
    "                  columns=pd.Index([\"left\", \"right\"], name=\"side\"))\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.661786Z",
     "end_time": "2024-04-18T23:03:50.670594Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "outputs": [
    {
     "data": {
      "text/plain": "side   left          right         \nstate  Ohio Colorado  Ohio Colorado\nnumber                             \none       0        3     5        8\ntwo       1        4     6        9\nthree     2        5     7       10",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th>side</th>\n      <th colspan=\"2\" halign=\"left\">left</th>\n      <th colspan=\"2\" halign=\"left\">right</th>\n    </tr>\n    <tr>\n      <th>state</th>\n      <th>Ohio</th>\n      <th>Colorado</th>\n      <th>Ohio</th>\n      <th>Colorado</th>\n    </tr>\n    <tr>\n      <th>number</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>one</th>\n      <td>0</td>\n      <td>3</td>\n      <td>5</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>two</th>\n      <td>1</td>\n      <td>4</td>\n      <td>6</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>three</th>\n      <td>2</td>\n      <td>5</td>\n      <td>7</td>\n      <td>10</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.unstack(level=\"state\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.680273Z",
     "end_time": "2024-04-18T23:03:50.670594Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "outputs": [
    {
     "data": {
      "text/plain": "state         Ohio  Colorado\nnumber side                 \none    left      0         3\n       right     5         8\ntwo    left      1         4\n       right     6         9\nthree  left      2         5\n       right     7        10",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>state</th>\n      <th>Ohio</th>\n      <th>Colorado</th>\n    </tr>\n    <tr>\n      <th>number</th>\n      <th>side</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">one</th>\n      <th>left</th>\n      <td>0</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>right</th>\n      <td>5</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">two</th>\n      <th>left</th>\n      <td>1</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>right</th>\n      <td>6</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">three</th>\n      <th>left</th>\n      <td>2</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>right</th>\n      <td>7</td>\n      <td>10</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.unstack(level=\"state\").stack(level=\"side\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.696720Z",
     "end_time": "2024-04-18T23:03:50.670594Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 将“长格式”旋转为“宽格式”"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "outputs": [
    {
     "data": {
      "text/plain": "     year  quarter   realgdp  realcons  realinv  realgovt  realdpi    cpi  \\\n0  1959.0      1.0  2710.349    1707.4  286.898   470.045   1886.9  28.98   \n1  1959.0      2.0  2778.801    1733.7  310.859   481.301   1919.7  29.15   \n2  1959.0      3.0  2775.488    1751.8  289.226   491.260   1916.4  29.35   \n3  1959.0      4.0  2785.204    1753.7  299.356   484.052   1931.3  29.37   \n4  1960.0      1.0  2847.699    1770.5  331.722   462.199   1955.5  29.54   \n\n      m1  tbilrate  unemp      pop  infl  realint  \n0  139.7      2.82    5.8  177.146  0.00     0.00  \n1  141.7      3.08    5.1  177.830  2.34     0.74  \n2  140.5      3.82    5.3  178.657  2.74     1.09  \n3  140.0      4.33    5.6  179.386  0.27     4.06  \n4  139.6      3.50    5.2  180.007  2.31     1.19  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>year</th>\n      <th>quarter</th>\n      <th>realgdp</th>\n      <th>realcons</th>\n      <th>realinv</th>\n      <th>realgovt</th>\n      <th>realdpi</th>\n      <th>cpi</th>\n      <th>m1</th>\n      <th>tbilrate</th>\n      <th>unemp</th>\n      <th>pop</th>\n      <th>infl</th>\n      <th>realint</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1959.0</td>\n      <td>1.0</td>\n      <td>2710.349</td>\n      <td>1707.4</td>\n      <td>286.898</td>\n      <td>470.045</td>\n      <td>1886.9</td>\n      <td>28.98</td>\n      <td>139.7</td>\n      <td>2.82</td>\n      <td>5.8</td>\n      <td>177.146</td>\n      <td>0.00</td>\n      <td>0.00</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1959.0</td>\n      <td>2.0</td>\n      <td>2778.801</td>\n      <td>1733.7</td>\n      <td>310.859</td>\n      <td>481.301</td>\n      <td>1919.7</td>\n      <td>29.15</td>\n      <td>141.7</td>\n      <td>3.08</td>\n      <td>5.1</td>\n      <td>177.830</td>\n      <td>2.34</td>\n      <td>0.74</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1959.0</td>\n      <td>3.0</td>\n      <td>2775.488</td>\n      <td>1751.8</td>\n      <td>289.226</td>\n      <td>491.260</td>\n      <td>1916.4</td>\n      <td>29.35</td>\n      <td>140.5</td>\n      <td>3.82</td>\n      <td>5.3</td>\n      <td>178.657</td>\n      <td>2.74</td>\n      <td>1.09</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1959.0</td>\n      <td>4.0</td>\n      <td>2785.204</td>\n      <td>1753.7</td>\n      <td>299.356</td>\n      <td>484.052</td>\n      <td>1931.3</td>\n      <td>29.37</td>\n      <td>140.0</td>\n      <td>4.33</td>\n      <td>5.6</td>\n      <td>179.386</td>\n      <td>0.27</td>\n      <td>4.06</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1960.0</td>\n      <td>1.0</td>\n      <td>2847.699</td>\n      <td>1770.5</td>\n      <td>331.722</td>\n      <td>462.199</td>\n      <td>1955.5</td>\n      <td>29.54</td>\n      <td>139.6</td>\n      <td>3.50</td>\n      <td>5.2</td>\n      <td>180.007</td>\n      <td>2.31</td>\n      <td>1.19</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('examples/macrodata.csv')\n",
    "data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.723091Z",
     "end_time": "2024-04-18T23:03:50.670594Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "outputs": [
    {
     "data": {
      "text/plain": "                           date     item     value\n0 1959-03-31 23:59:59.999999999  realgdp  2710.349\n1 1959-03-31 23:59:59.999999999     infl     0.000\n2 1959-03-31 23:59:59.999999999    unemp     5.800\n3 1959-06-30 23:59:59.999999999  realgdp  2778.801\n4 1959-06-30 23:59:59.999999999     infl     2.340\n5 1959-06-30 23:59:59.999999999    unemp     5.100\n6 1959-09-30 23:59:59.999999999  realgdp  2775.488\n7 1959-09-30 23:59:59.999999999     infl     2.740\n8 1959-09-30 23:59:59.999999999    unemp     5.300\n9 1959-12-31 23:59:59.999999999  realgdp  2785.204",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>date</th>\n      <th>item</th>\n      <th>value</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1959-03-31 23:59:59.999999999</td>\n      <td>realgdp</td>\n      <td>2710.349</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1959-03-31 23:59:59.999999999</td>\n      <td>infl</td>\n      <td>0.000</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1959-03-31 23:59:59.999999999</td>\n      <td>unemp</td>\n      <td>5.800</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1959-06-30 23:59:59.999999999</td>\n      <td>realgdp</td>\n      <td>2778.801</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1959-06-30 23:59:59.999999999</td>\n      <td>infl</td>\n      <td>2.340</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>1959-06-30 23:59:59.999999999</td>\n      <td>unemp</td>\n      <td>5.100</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>1959-09-30 23:59:59.999999999</td>\n      <td>realgdp</td>\n      <td>2775.488</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>1959-09-30 23:59:59.999999999</td>\n      <td>infl</td>\n      <td>2.740</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>1959-09-30 23:59:59.999999999</td>\n      <td>unemp</td>\n      <td>5.300</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>1959-12-31 23:59:59.999999999</td>\n      <td>realgdp</td>\n      <td>2785.204</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "periods = pd.PeriodIndex(year=data.year, quarter=data.quarter, name='date')\n",
    "columns = pd.Index(['realgdp', 'infl', 'unemp'], name='item')\n",
    "data = data.reindex(columns=columns)\n",
    "data.index = periods.to_timestamp('D', 'end')\n",
    "ldata = data.stack().reset_index().rename(columns={0: 'value'})\n",
    "ldata[:10]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.749372Z",
     "end_time": "2024-04-18T23:03:50.670594Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "outputs": [
    {
     "data": {
      "text/plain": "item                           infl   realgdp  unemp\ndate                                                \n1959-03-31 23:59:59.999999999  0.00  2710.349    5.8\n1959-06-30 23:59:59.999999999  2.34  2778.801    5.1\n1959-09-30 23:59:59.999999999  2.74  2775.488    5.3\n1959-12-31 23:59:59.999999999  0.27  2785.204    5.6\n1960-03-31 23:59:59.999999999  2.31  2847.699    5.2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>item</th>\n      <th>infl</th>\n      <th>realgdp</th>\n      <th>unemp</th>\n    </tr>\n    <tr>\n      <th>date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1959-03-31 23:59:59.999999999</th>\n      <td>0.00</td>\n      <td>2710.349</td>\n      <td>5.8</td>\n    </tr>\n    <tr>\n      <th>1959-06-30 23:59:59.999999999</th>\n      <td>2.34</td>\n      <td>2778.801</td>\n      <td>5.1</td>\n    </tr>\n    <tr>\n      <th>1959-09-30 23:59:59.999999999</th>\n      <td>2.74</td>\n      <td>2775.488</td>\n      <td>5.3</td>\n    </tr>\n    <tr>\n      <th>1959-12-31 23:59:59.999999999</th>\n      <td>0.27</td>\n      <td>2785.204</td>\n      <td>5.6</td>\n    </tr>\n    <tr>\n      <th>1960-03-31 23:59:59.999999999</th>\n      <td>2.31</td>\n      <td>2847.699</td>\n      <td>5.2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pivoted = ldata.pivot(index=\"date\", columns=\"item\", values=\"value\")\n",
    "pivoted.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.780987Z",
     "end_time": "2024-04-18T23:03:50.670594Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "outputs": [
    {
     "data": {
      "text/plain": "                           date     item     value    value2\n0 1959-03-31 23:59:59.999999999  realgdp  2710.349  0.523772\n1 1959-03-31 23:59:59.999999999     infl     0.000  0.000940\n2 1959-03-31 23:59:59.999999999    unemp     5.800  1.343810\n3 1959-06-30 23:59:59.999999999  realgdp  2778.801 -0.713544\n4 1959-06-30 23:59:59.999999999     infl     2.340 -0.831154\n5 1959-06-30 23:59:59.999999999    unemp     5.100 -2.370232\n6 1959-09-30 23:59:59.999999999  realgdp  2775.488 -1.860761\n7 1959-09-30 23:59:59.999999999     infl     2.740 -0.860757\n8 1959-09-30 23:59:59.999999999    unemp     5.300  0.560145\n9 1959-12-31 23:59:59.999999999  realgdp  2785.204 -1.265934",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>date</th>\n      <th>item</th>\n      <th>value</th>\n      <th>value2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1959-03-31 23:59:59.999999999</td>\n      <td>realgdp</td>\n      <td>2710.349</td>\n      <td>0.523772</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1959-03-31 23:59:59.999999999</td>\n      <td>infl</td>\n      <td>0.000</td>\n      <td>0.000940</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1959-03-31 23:59:59.999999999</td>\n      <td>unemp</td>\n      <td>5.800</td>\n      <td>1.343810</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1959-06-30 23:59:59.999999999</td>\n      <td>realgdp</td>\n      <td>2778.801</td>\n      <td>-0.713544</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1959-06-30 23:59:59.999999999</td>\n      <td>infl</td>\n      <td>2.340</td>\n      <td>-0.831154</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>1959-06-30 23:59:59.999999999</td>\n      <td>unemp</td>\n      <td>5.100</td>\n      <td>-2.370232</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>1959-09-30 23:59:59.999999999</td>\n      <td>realgdp</td>\n      <td>2775.488</td>\n      <td>-1.860761</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>1959-09-30 23:59:59.999999999</td>\n      <td>infl</td>\n      <td>2.740</td>\n      <td>-0.860757</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>1959-09-30 23:59:59.999999999</td>\n      <td>unemp</td>\n      <td>5.300</td>\n      <td>0.560145</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>1959-12-31 23:59:59.999999999</td>\n      <td>realgdp</td>\n      <td>2785.204</td>\n      <td>-1.265934</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ldata[\"value2\"] = np.random.standard_normal(len(ldata))\n",
    "ldata[:10]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.798318Z",
     "end_time": "2024-04-18T23:03:50.670594Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "outputs": [
    {
     "data": {
      "text/plain": "                              value                    value2            \\\nitem                           infl   realgdp unemp      infl   realgdp   \ndate                                                                      \n1959-03-31 23:59:59.999999999  0.00  2710.349   5.8  0.000940  0.523772   \n1959-06-30 23:59:59.999999999  2.34  2778.801   5.1 -0.831154 -0.713544   \n1959-09-30 23:59:59.999999999  2.74  2775.488   5.3 -0.860757 -1.860761   \n1959-12-31 23:59:59.999999999  0.27  2785.204   5.6  0.119827 -1.265934   \n1960-03-31 23:59:59.999999999  2.31  2847.699   5.2 -2.359419  0.332883   \n\n                                         \nitem                              unemp  \ndate                                     \n1959-03-31 23:59:59.999999999  1.343810  \n1959-06-30 23:59:59.999999999 -2.370232  \n1959-09-30 23:59:59.999999999  0.560145  \n1959-12-31 23:59:59.999999999 -1.063512  \n1960-03-31 23:59:59.999999999 -0.199543  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th colspan=\"3\" halign=\"left\">value</th>\n      <th colspan=\"3\" halign=\"left\">value2</th>\n    </tr>\n    <tr>\n      <th>item</th>\n      <th>infl</th>\n      <th>realgdp</th>\n      <th>unemp</th>\n      <th>infl</th>\n      <th>realgdp</th>\n      <th>unemp</th>\n    </tr>\n    <tr>\n      <th>date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1959-03-31 23:59:59.999999999</th>\n      <td>0.00</td>\n      <td>2710.349</td>\n      <td>5.8</td>\n      <td>0.000940</td>\n      <td>0.523772</td>\n      <td>1.343810</td>\n    </tr>\n    <tr>\n      <th>1959-06-30 23:59:59.999999999</th>\n      <td>2.34</td>\n      <td>2778.801</td>\n      <td>5.1</td>\n      <td>-0.831154</td>\n      <td>-0.713544</td>\n      <td>-2.370232</td>\n    </tr>\n    <tr>\n      <th>1959-09-30 23:59:59.999999999</th>\n      <td>2.74</td>\n      <td>2775.488</td>\n      <td>5.3</td>\n      <td>-0.860757</td>\n      <td>-1.860761</td>\n      <td>0.560145</td>\n    </tr>\n    <tr>\n      <th>1959-12-31 23:59:59.999999999</th>\n      <td>0.27</td>\n      <td>2785.204</td>\n      <td>5.6</td>\n      <td>0.119827</td>\n      <td>-1.265934</td>\n      <td>-1.063512</td>\n    </tr>\n    <tr>\n      <th>1960-03-31 23:59:59.999999999</th>\n      <td>2.31</td>\n      <td>2847.699</td>\n      <td>5.2</td>\n      <td>-2.359419</td>\n      <td>0.332883</td>\n      <td>-0.199543</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pivoted = ldata.pivot(index=\"date\", columns=\"item\")\n",
    "pivoted.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.818862Z",
     "end_time": "2024-04-18T23:03:50.670594Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "outputs": [
    {
     "data": {
      "text/plain": "item                           infl   realgdp  unemp\ndate                                                \n1959-03-31 23:59:59.999999999  0.00  2710.349    5.8\n1959-06-30 23:59:59.999999999  2.34  2778.801    5.1\n1959-09-30 23:59:59.999999999  2.74  2775.488    5.3\n1959-12-31 23:59:59.999999999  0.27  2785.204    5.6\n1960-03-31 23:59:59.999999999  2.31  2847.699    5.2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>item</th>\n      <th>infl</th>\n      <th>realgdp</th>\n      <th>unemp</th>\n    </tr>\n    <tr>\n      <th>date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1959-03-31 23:59:59.999999999</th>\n      <td>0.00</td>\n      <td>2710.349</td>\n      <td>5.8</td>\n    </tr>\n    <tr>\n      <th>1959-06-30 23:59:59.999999999</th>\n      <td>2.34</td>\n      <td>2778.801</td>\n      <td>5.1</td>\n    </tr>\n    <tr>\n      <th>1959-09-30 23:59:59.999999999</th>\n      <td>2.74</td>\n      <td>2775.488</td>\n      <td>5.3</td>\n    </tr>\n    <tr>\n      <th>1959-12-31 23:59:59.999999999</th>\n      <td>0.27</td>\n      <td>2785.204</td>\n      <td>5.6</td>\n    </tr>\n    <tr>\n      <th>1960-03-31 23:59:59.999999999</th>\n      <td>2.31</td>\n      <td>2847.699</td>\n      <td>5.2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pivoted[\"value\"].head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.861520Z",
     "end_time": "2024-04-18T23:03:50.670594Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "outputs": [
    {
     "data": {
      "text/plain": "                              value                    value2            \\\nitem                           infl   realgdp unemp      infl   realgdp   \ndate                                                                      \n1959-03-31 23:59:59.999999999  0.00  2710.349   5.8  0.000940  0.523772   \n1959-06-30 23:59:59.999999999  2.34  2778.801   5.1 -0.831154 -0.713544   \n1959-09-30 23:59:59.999999999  2.74  2775.488   5.3 -0.860757 -1.860761   \n1959-12-31 23:59:59.999999999  0.27  2785.204   5.6  0.119827 -1.265934   \n1960-03-31 23:59:59.999999999  2.31  2847.699   5.2 -2.359419  0.332883   \n\n                                         \nitem                              unemp  \ndate                                     \n1959-03-31 23:59:59.999999999  1.343810  \n1959-06-30 23:59:59.999999999 -2.370232  \n1959-09-30 23:59:59.999999999  0.560145  \n1959-12-31 23:59:59.999999999 -1.063512  \n1960-03-31 23:59:59.999999999 -0.199543  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th colspan=\"3\" halign=\"left\">value</th>\n      <th colspan=\"3\" halign=\"left\">value2</th>\n    </tr>\n    <tr>\n      <th>item</th>\n      <th>infl</th>\n      <th>realgdp</th>\n      <th>unemp</th>\n      <th>infl</th>\n      <th>realgdp</th>\n      <th>unemp</th>\n    </tr>\n    <tr>\n      <th>date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1959-03-31 23:59:59.999999999</th>\n      <td>0.00</td>\n      <td>2710.349</td>\n      <td>5.8</td>\n      <td>0.000940</td>\n      <td>0.523772</td>\n      <td>1.343810</td>\n    </tr>\n    <tr>\n      <th>1959-06-30 23:59:59.999999999</th>\n      <td>2.34</td>\n      <td>2778.801</td>\n      <td>5.1</td>\n      <td>-0.831154</td>\n      <td>-0.713544</td>\n      <td>-2.370232</td>\n    </tr>\n    <tr>\n      <th>1959-09-30 23:59:59.999999999</th>\n      <td>2.74</td>\n      <td>2775.488</td>\n      <td>5.3</td>\n      <td>-0.860757</td>\n      <td>-1.860761</td>\n      <td>0.560145</td>\n    </tr>\n    <tr>\n      <th>1959-12-31 23:59:59.999999999</th>\n      <td>0.27</td>\n      <td>2785.204</td>\n      <td>5.6</td>\n      <td>0.119827</td>\n      <td>-1.265934</td>\n      <td>-1.063512</td>\n    </tr>\n    <tr>\n      <th>1960-03-31 23:59:59.999999999</th>\n      <td>2.31</td>\n      <td>2847.699</td>\n      <td>5.2</td>\n      <td>-2.359419</td>\n      <td>0.332883</td>\n      <td>-0.199543</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "unstacked = ldata.set_index([\"date\", \"item\"]).unstack(level=\"item\")\n",
    "unstacked.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.884449Z",
     "end_time": "2024-04-18T23:03:50.703306Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 将“宽格式”旋转为“长格式”"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "outputs": [
    {
     "data": {
      "text/plain": "   key  A  B  C\n0  foo  1  4  7\n1  bar  2  5  8\n2  baz  3  6  9",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>foo</td>\n      <td>1</td>\n      <td>4</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>bar</td>\n      <td>2</td>\n      <td>5</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>baz</td>\n      <td>3</td>\n      <td>6</td>\n      <td>9</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({\"key\": [\"foo\", \"bar\", \"baz\"],\n",
    "                   \"A\": [1, 2, 3],\n",
    "                   \"B\": [4, 5, 6],\n",
    "                   \"C\": [7, 8, 9]})\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.924859Z",
     "end_time": "2024-04-18T23:03:50.703306Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "outputs": [
    {
     "data": {
      "text/plain": "   key variable  value\n0  foo        A      1\n1  bar        A      2\n2  baz        A      3\n3  foo        B      4\n4  bar        B      5\n5  baz        B      6\n6  foo        C      7\n7  bar        C      8\n8  baz        C      9",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>variable</th>\n      <th>value</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>foo</td>\n      <td>A</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>bar</td>\n      <td>A</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>baz</td>\n      <td>A</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>foo</td>\n      <td>B</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>bar</td>\n      <td>B</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>baz</td>\n      <td>B</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>foo</td>\n      <td>C</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>bar</td>\n      <td>C</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>baz</td>\n      <td>C</td>\n      <td>9</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "melted = pd.melt(df, id_vars=\"key\")\n",
    "melted"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.957829Z",
     "end_time": "2024-04-18T23:03:50.720980Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "outputs": [
    {
     "data": {
      "text/plain": "variable  A  B  C\nkey              \nbar       2  5  8\nbaz       3  6  9\nfoo       1  4  7",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>variable</th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n    </tr>\n    <tr>\n      <th>key</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>bar</th>\n      <td>2</td>\n      <td>5</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>baz</th>\n      <td>3</td>\n      <td>6</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>foo</th>\n      <td>1</td>\n      <td>4</td>\n      <td>7</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reshaped = melted.pivot(index=\"key\", columns=\"variable\",\n",
    "                        values=\"value\")\n",
    "reshaped"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:49.990630Z",
     "end_time": "2024-04-18T23:03:50.720980Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "outputs": [
    {
     "data": {
      "text/plain": "variable  key  A  B  C\n0         bar  2  5  8\n1         baz  3  6  9\n2         foo  1  4  7",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>variable</th>\n      <th>key</th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>bar</td>\n      <td>2</td>\n      <td>5</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>baz</td>\n      <td>3</td>\n      <td>6</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>foo</td>\n      <td>1</td>\n      <td>4</td>\n      <td>7</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reshaped.reset_index()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:50.004607Z",
     "end_time": "2024-04-18T23:03:50.720980Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "outputs": [
    {
     "data": {
      "text/plain": "   key variable  value\n0  foo        A      1\n1  bar        A      2\n2  baz        A      3\n3  foo        B      4\n4  bar        B      5\n5  baz        B      6",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>variable</th>\n      <th>value</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>foo</td>\n      <td>A</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>bar</td>\n      <td>A</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>baz</td>\n      <td>A</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>foo</td>\n      <td>B</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>bar</td>\n      <td>B</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>baz</td>\n      <td>B</td>\n      <td>6</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.melt(df, id_vars=\"key\", value_vars=[\"A\", \"B\"])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:50.018090Z",
     "end_time": "2024-04-18T23:03:50.774448Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "outputs": [
    {
     "data": {
      "text/plain": "  variable  value\n0        A      1\n1        A      2\n2        A      3\n3        B      4\n4        B      5\n5        B      6\n6        C      7\n7        C      8\n8        C      9",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>variable</th>\n      <th>value</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>A</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>A</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>A</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>B</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>B</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>B</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>C</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>C</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>C</td>\n      <td>9</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.melt(df, value_vars=[\"A\", \"B\", \"C\"])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:50.054832Z",
     "end_time": "2024-04-18T23:03:50.774448Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "outputs": [
    {
     "data": {
      "text/plain": "  variable value\n0      key   foo\n1      key   bar\n2      key   baz\n3        A     1\n4        A     2\n5        A     3\n6        B     4\n7        B     5\n8        B     6",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>variable</th>\n      <th>value</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>key</td>\n      <td>foo</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>key</td>\n      <td>bar</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>key</td>\n      <td>baz</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>A</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>A</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>A</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>B</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>B</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>B</td>\n      <td>6</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.melt(df, value_vars=[\"key\", \"A\", \"B\"])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:50.083371Z",
     "end_time": "2024-04-18T23:03:50.802428Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-18T23:03:50.104560Z",
     "end_time": "2024-04-18T23:03:50.802428Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
